package com.yby.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.yby.common.utils.PageUtils;
import com.yby.common.utils.R;
import com.yby.mall.product.dao.AttrAttrgroupRelationDao;
import com.yby.mall.product.dao.CategoryDao;
import com.yby.mall.product.dao.ProductAttrValueDao;
import com.yby.mall.product.dto.AttrInfoDto;
import com.yby.mall.product.entity.AttrAttrgroupRelationEntity;
import com.yby.mall.product.entity.ProductAttrValueEntity;
import com.yby.mall.product.service.AttrGroupService;
import com.yby.mall.product.vo.AttrVo;
import com.yby.mall.product.vo.ProductAttrValueVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yby.common.utils.Query;

import com.yby.mall.product.dao.AttrDao;
import com.yby.mall.product.entity.AttrEntity;
import com.yby.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrGroupServiceImpl attrGroupServiceImpl;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Override
    public R queryPage(Long catId, String type, Integer page, Integer limit, String key) {
        PageHelper.startPage(page,limit);
        List<AttrInfoDto> list = attrDao.queryPage(catId, type, key);
        return R.ok().put("page", PageUtils.restPage(list));
    }

    @Override
    public R info(Long attrId) {
        AttrEntity attrEntity = attrDao.selectById(attrId);
        List<Long> list = new ArrayList<>();
        if(attrEntity.getCatelogId() != null){
            attrGroupServiceImpl.findByParentId(attrEntity.getCatelogId(), list);
        }
        attrEntity.setCatelogPath(list);
        //查找当前属性对应组
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery().eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
        if(attrAttrgroupRelationEntity != null){
            attrEntity.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
        }else {
            attrEntity.setAttrGroupId(null);
        }
        return R.ok().put("attr",attrEntity);
    }

    @Override
    @Transactional
    public R modify(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity,"attrGroupId");
        attrDao.updateById(attrEntity);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrVo.getAttrId());
        attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        if(attrVo.getAttrGroupId() != null){
            if(attrAttrgroupRelationDao.selectCount(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery().eq(AttrAttrgroupRelationEntity::getAttrId,attrVo.getAttrId()).eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrVo.getAttrGroupId()))>0){
                attrAttrgroupRelationDao.update(attrAttrgroupRelationEntity,Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery().eq(AttrAttrgroupRelationEntity::getAttrId,attrVo.getAttrId()).eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrVo.getAttrGroupId()));
            }else{
                attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
            }
        }
        return R.ok();
    }

    @Override
    public R saveAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity,"attrGroupId");
        attrDao.insert(attrEntity);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
        attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        if(attrVo.getAttrGroupId() != null){
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
        return R.ok();
    }

    @Override
    public R deleteByIds(List<Long> asList) {
        attrDao.deleteBatchIds(asList);
        for (Long attrId : asList) {
            attrAttrgroupRelationDao.delete(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery().eq(AttrAttrgroupRelationEntity::getAttrId,attrId));
        }
        return R.ok();
    }

    @Override
    public R baseListforSpu(Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueDao.selectList(Wrappers.<ProductAttrValueEntity>lambdaQuery().eq(ProductAttrValueEntity::getSpuId, spuId));
        return R.ok().put("data",list);
    }

    @Override
    public R updateProductAttrVal(List<ProductAttrValueVo> list,Long spuId) {
        productAttrValueDao.delete(Wrappers.<ProductAttrValueEntity>lambdaQuery().eq(ProductAttrValueEntity::getSpuId,spuId));
        list.stream().forEach(productAttrValueVo -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            BeanUtils.copyProperties(productAttrValueVo,productAttrValueEntity);
            productAttrValueEntity.setSpuId(spuId);
            productAttrValueDao.insert(productAttrValueEntity);
        });
        return R.ok();
    }
}